package za.ac.wits.elen7045.persistence.datasource.sqlite;

import java.sql.ResultSet;
import java.sql.SQLException;
import za.ac.wits.elen7045.persistence.datasource.DataSourceRowEntry;
import za.ac.wits.elen7045.persistence.model.Account;
import za.ac.wits.elen7045.persistence.model.ServiceProviderName;
import za.ac.wits.elen7045.persistence.util.AccountTableConstants;
import za.ac.wits.elen7045.persistence.util.ServiceProviderTableConstants;

public class AccountSQLiteDataSource extends SQLiteDataSource<Account> implements AccountTableConstants {

	private final String SERVICE_PROVIDER_TYPE = ServiceProviderTableConstants.SERVICE_PROVIDER_TYPE;
	
	@Override
	protected Account parseCurrentRowToObject(ResultSet resultSet) throws SQLException {
		ServiceProviderName serviceProviderType = ServiceProviderName.getServiceProviderTypeByName(resultSet.getString(SERVICE_PROVIDER_TYPE));
		int accountNumber = resultSet.getInt(ACCOUNT_NUMBER);
		Account account = new Account(serviceProviderType, accountNumber);
		account.setId(resultSet.getInt(ACCOUNT_ID));
		account.setAccountOwnerName(resultSet.getString(ACCOUNT_OWNER_NAME));
		return account;
	}

	@Override
	protected DataSourceRowEntry parseObjectToDataSourceRowEntry(Account account) throws SQLException {
		DataSourceRowEntry entry = new DataSourceRowEntry();
		entry.setValue(ACCOUNT_ID, account.getId());
		entry.setValue(ACCOUNT_NUMBER, account.getAccountNumber());
		entry.setValue(ACCOUNT_OWNER_NAME, account.getAccountOwnerName());
		entry.setValue(SERVICE_PROVIDER_TYPE, account.getServiceProviderType().getName());
		return entry;
	}

}
